/*
 * Date         Author       Note
 * 2015.04.29   舞者无罪              辛克方向
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "sink_drt.h"
#include "com.h"
#include "keypane.h"
#include "info.h"
#include "sys.h"

/*
 *
 */
static void CreateSinkDrtInterface(HWND hWnd)
{
    char sc[64] = {0};
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_TIME_BMP - CX_ASK_BMP - 10,
        8,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow(MH_LABEL_NAME,
        GetString(121),
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        40,
        65,
        100,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_GREEN);
    CreateWindow(MH_EDIT_NAME,
        GetSinkEnable(g_knit_data) ? MW_OPEN_CAPTION : MW_CLOSE_CAPTION,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_SINK_ENABLE,
        160,
        65,
        60,
        20,
        hWnd, 0);
    //
    CreateWindow(MH_LABEL_NAME,
        GetString(638),
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        40,
        95,
        100,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_GREEN);
    CreateWindow(MH_EDIT_NAME,
        GetSinkDrt(g_knit_data) ? GetString(618) : GetString(617),
        WS_CHILD | WS_VISIBLE,
        IDC_SINK,
        160,
        95,
        60,
        20,
        hWnd, 0);
    CreateWindow(MH_LABEL_NAME,
        GetString(640),
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        40,
        125,
        100,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_GREEN);
    sprintf(sc, "%u", GetSinkWorkSpeed(g_knit_data));
    CreateWindow(MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_SPEED,
        160,
        125,
        60,
        20,
        hWnd, 0);
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    uchar n = atoi(sc);
    SetSinkWorkSpeed(g_knit_data, n);
    SetWindowAdditionalData(hWnd, TRUE);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
     if(nID == ID_ESC)
    {
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        if(GetWindowAdditionalData(hWnd))
        {
            int ret = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
            if(ret)
            {
                printf("write sink drt: %d\n", ret);
                AfxInfoBox(hWnd, NULL, GetString(481));
            }
            else
            {
                MH_FILE_SYNC();
            }
            SendSysParam(g_knit_data);
        }
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
    }
    else if(key == SCANCODE_ENTER)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID == IDC_EDIT_SINK_ENABLE)
        {
            uchar n = GetSinkEnable(g_knit_data);
            n = !n;
            SetSinkEnable(g_knit_data, n);
            SetWindowCaption(GetDlgItem(hWnd, nID), n ? MW_OPEN_CAPTION : MW_CLOSE_CAPTION);
            InvalidateRect(GetDlgItem(hWnd, nID), NULL, TRUE);
            SetWindowAdditionalData(hWnd, TRUE);
            FlushSysWndSinkEnable(GetHosting(hWnd));
        }
        else if(nID == IDC_SINK)
        {
            int n = GetSinkDrt(g_knit_data);
            n = !n;
            SetSinkDrt(g_knit_data, n);
            HWND hTmp = GetDlgItem(hWnd, nID);
            SetWindowCaption(hTmp, n ? GetString(618) : GetString(617));
            InvalidateRect(hTmp, NULL, TRUE);
            SetWindowAdditionalData(hWnd, TRUE);
        }
        else if(nID == IDC_SPEED)
        {
            HWND hTmp = GetDlgItem(hWnd, nID);
            RECT rect;
            GetClientRect(hTmp, &rect);
            ClientToScreen(hTmp, &rect.left, &rect.top);
            ClientToScreen(hTmp, &rect.right, &rect.bottom);
            int value = atoi(GetWindowCaption(hTmp));
            CreateKeyboardWindow(hWnd, rect, 1, 100, value);
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID == IDC_SINK)
        {
            nID = IDC_EDIT_SINK_ENABLE;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
        else if(nID == IDC_SPEED)
        {
            nID = IDC_SINK;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID == IDC_EDIT_SINK_ENABLE)
        {
            nID = IDC_SINK;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
        else if(nID == IDC_SINK)
        {
            nID = IDC_SPEED;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
}
/*
 *
 */
static int SinkDrtWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateSinkDrtInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        break;
    case MSG_CHAR:
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_TIME]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateSinkDrtWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_TIME_BMP) / 2;
    int y = (CY_SCREEN - CY_TIME_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(639);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = SinkDrtWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_TIME_BMP;
    CreateInfo.by = y + CY_TIME_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT_SINK_ENABLE));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}

